// 嘿嘿
(function () {

    var _YK_VC_OPEN_ = '_YK_VC_OPEN_'

    var shouldOpen = localStorage.getItem(_YK_VC_OPEN_) === _YK_VC_OPEN_

    if (shouldOpen) {
        open()
    } else {
        lock()
    }

    function open() {
        var vConsole = new VConsole();
    }

    function lock() {
        /*
                打开条件
                V	．．．━
                C	━ ．━ ．
            */
        var TYPES = {
            tap: 'tap',
            press: 'press'
        }
        // 开启条件
        var success = [
            TYPES.tap,
            TYPES.tap,
            TYPES.tap,
            TYPES.press,
            TYPES.press,
            TYPES.tap,
            TYPES.press,
            TYPES.tap
        ]
        // action策略
        var Strategy = {
            [TYPES.tap]: function onTap() {
                actionList.push(TYPES.tap)
            },
            [TYPES.press]: function onPress() {
                actionList.push(TYPES.press)
            }
        }
        // action记录
        var actionList = []
        var tapDuration = [0, 500]
        var pressDuration = [3000, 10000]

        var lastDownTime = 0

        function onAction(currType) {
            try {
                const action = Strategy[currType]
                if (typeof action !== 'function') {
                    throw new Error('不正确的点击')
                }
                if (!check(currType)) {
                    throw new Error('输入错误')
                }
                action()
                // console.log('键入', currType)
                if (actionList.length === success.length) {
                    unlock()
                }
            } catch (e) {
                // console.log(e.message)
                actionList = []
            }
        }

        function check(currType) {
            const curIndex = actionList.length
            return currType === success[curIndex]
        }

        function onDown() {
            lastDownTime = Date.now()
        }

        function onUp(e) {
            var duration = Date.now() - lastDownTime
            if (duration < tapDuration[1]) {
                onAction(TYPES.tap)
            } else if (duration > pressDuration[0] && duration < pressDuration[1]) {
                onAction(TYPES.press)
            } else {
                onAction('other')
            }
        }

        function unlock() {
            alert('行，你破解了密码，进入debug模式吧')
            window.removeEventListener('touchstart', onDown)
            window.removeEventListener('touchend', onUp)
            // 存localstorage
            localStorage.setItem(_YK_VC_OPEN_, _YK_VC_OPEN_)
            open()
        }

        window.addEventListener('touchstart', onDown)
        window.addEventListener('touchend', onUp)
    }
})()
